﻿Imports System
Imports System.Threading

Namespace System.Workflow.Runtime
    Friend Class DbRetry
        ' Methods
        Protected Sub New()
            Me._maxRetries = 20
            Me._retrySleep = &H7D0
        End Sub

        Friend Sub New(ByVal enableRetries As Boolean)
            Me._maxRetries = 20
            Me._retrySleep = &H7D0
            Me._enableRetries = enableRetries
        End Sub

        Friend Function CanRetry(ByVal retryCount As Short) As Boolean
            If Not Me._enableRetries Then
                Return False
            End If
            Return (retryCount < Me._maxRetries)
        End Function

        Friend Sub RetrySleep(ByVal retryCount As Short)
            If (retryCount > 3) Then
                Dim millisecondsTimeout As Integer = (Me._retrySleep * retryCount)
                Thread.Sleep(millisecondsTimeout)
            End If
        End Sub

        Friend Function TryDoRetry(ByRef retryCount As Short) As Boolean
            Dim num As Short
            retryCount = CShort((num = retryCount + 1))
            If Me.CanRetry(num) Then
                Me.RetrySleep(retryCount)
                Return True
            End If
            Return False
        End Function


        ' Properties
        Friend ReadOnly Property MaxRetries As Short
            Get
                Return Me._maxRetries
            End Get
        End Property


        ' Fields
        Private Const _defaultMaxRetries As Short = 20
        Private Const _defaultRetrySleep As Integer = &H7D0
        Private _enableRetries As Boolean
        Private _maxRetries As Short
        Private _retrySleep As Integer
        Private Const _spinCount As Short = 3
    End Class
End Namespace

